<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'客户列表'),
);
$this->model('model')->checkjuese(3);
$juese_id=$_SESSION['juese_id']==3||$_SESSION['juese_id']==4?$_SESSION['juese_id']:4;
$workerids=$this->model('model')->getworkerids($juese_id,true);
$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['workers']=$this->getall('worker',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
//客户列表
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$return['params']=array();
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$params=array('worker'.$juese_id.'id'=>$workerids,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['jibie_type'] && $_RQ['jibie_type']!='all'){
		$params['jibie_type']=intval($_RQ['jibie_type']);
	}
	$return['params']['jibie_type']=$_RQ['jibie_type']?:'all';
	if($_RQ['status']!='all' && $_RQ['status']!='isall'){
		if($_RQ['status']=='shangmen'){
			$params['zhun_time >']=0;
			$return['params']['status']='shangmen';
		}else{
			$params['status']=intval($_RQ['status']);
			$return['params']['status']=intval($_RQ['status']);
		}
	}elseif($_RQ['status']=='isall'){
		$params['status >']=0;
		$return['params']['status']='isall';
	}else{
		$return['params']['status']='all';
	}
	if($_RQ['from_type']){
		$params['from_type']=$_SESSION['store_type']==3?2:1;
		$return['params']['from_type']=intval($_RQ['from_type']);
		if($params['status'] && $params['status']!=100 && $params['status']!=110){
			$batch=array();
		}elseif($_SESSION['store_type']!=3){
			$batch=array(array('type'=>'modal','url'=>'/juese3.list/worker5id/','title'=>'一键分配电客员'));
		}else{
			$batch=array(array('type'=>'modal','url'=>'/juese3.list/worker6id/','title'=>'一键分配销售员'));
		}
	}else{
		$batch=array();
	}
	$batch[]=array('type'=>'api','url'=>'/juese3.list/export2/','title'=>'指定导出数据');
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		if($_RQ['status']=='shangmen'){
			$params['zhun_time between']=array($time_range['tstart'],$time_range['tend']);
		}else{
			$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		}
		$return['params']['createtime']=$time_range;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:40;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['id']);
			if($item['from_type']==1){
				$item['from_typetitle']=$this->member_from1_type[$item['from1_type']];
			}elseif($item['from_type']==2){
				$item['from_typetitle']=$this->member_from2_type[$item['from2_type']];
			}else{
				$item['from_typetitle']=$this->member_from_type[$item['from_type']];
			}
			if($_SESSION['juese_id']==3){
				$this->model('uplog')->upworkerlog($item,0,true);
			}elseif($_SESSION['juese_id']==4){
				$this->model('uplog')->upworkerlog($item,0,true);
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、这里显示自己添加的客户或公海池里面的客户！只有【线下来源】的需要分配电客员
		','del_message'=>'确认删除该客户吗？','total'=>$total,'psize'=>$psize,'del_op'=>'del','del_one'=>false,'delall'=>false,'edit'=>false,'html_to'=>'list_html','list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			//array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'jibie_type','title'=>'客户级别','type'=>'radio2','options'=>'jibie_type'),
			array('name'=>'from_typetitle','title'=>'渠道','type'=>'string'),
			array('title'=>'网络员','name'=>'worker3id','type'=>'select2','options'=>'workers'),
			array('title'=>'前台专员','name'=>'worker4id','type'=>'select2','options'=>'workers'),
			array('title'=>'电客员','name'=>'worker5id','type'=>'select2','options'=>'workers'),
			array('title'=>'销售员','name'=>'worker6id','type'=>'select2','options'=>'workers'),
			array('title'=>'体验员','name'=>'worker7id','type'=>'select2','options'=>'workers'),
			array('title'=>'试用员','name'=>'worker8id','type'=>'select2','options'=>'workers'),
			array('name'=>'status','title'=>'状态','type'=>'radio2','options'=>'status'),
			array('name'=>'createtime','title'=>'添加日期','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','action'=>'jy_yxw/juese3.list/worker5id/','title'=>'分配电客员','show'=>array('status'=>array(0,100,110),'from_type'=>1)),
			array('type'=>'modal','action'=>'jy_yxw/juese3.list/setstatus4','title'=>'处理已意向','show'=>array('status'=>array(3))),
			array('type'=>'modal','action'=>'jy_yxw/juese3.list/worker6id/','title'=>'分配销售员','show'=>array('status'=>4)),
			array('type'=>'modal','action'=>'jy_yxw/juese3.list/worker6id/','title'=>'分配销售员','show'=>array('status'=>0,'from_type'=>2)),
			array('type'=>'modal','action'=>'jy_yxw/juese3.list/worker5id/','title'=>'重新分配电客员','show'=>array('status'=>1)),
			array('type'=>'modal','action'=>'jy_yxw/juese3.list/worker6id/','title'=>'重新分配销售员','show'=>array('status'=>32)),
			array('type'=>'href','action'=>'jy_yxw/juese3.list/edit/','title'=>'完善客户信息','show'=>array('status'=>array(0))),
			array('type'=>'post','action'=>'jy_yxw/juese3.list/del/','title'=>'删除','show'=>array('status'=>array(0))),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'10','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'10','title'=>'客户名称','name'=>'title','help'=>''),
			array('name'=>'status','width'=>'10','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','width'=>15,'title'=>'时间段','type'=>'time_range')
		),
	);
	if($_RQ['from_type']){
		if($params['status']){
			
		}elseif($params['from_type']==1){
			unset($htmls['list']['list'][4]);
		}else{
			unset($htmls['list']['list'][3]);
		}
	}
	$return['status']=$this->member_status1;
	$return['status']['all']='全部';
	$return['jibie_type']=$this->member_jibie_type;
	$return['from_type']=$this->member_from_type;
	$return['from1_type']=$this->member_from1_type;
	$return['from2_type']=$this->member_from2_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->model('member')->add($postdata);
		exi('操作成功','','/juese3.list//status=all');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		if($item['birth']){
			$item['age']=date('Y')-$item['birth'];
		}
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'tabs'=>$tabs,
		'edit'=>$this->model('member')->edit_item(),
	);
	$return['sex']=$this->sex;
	$return['xueli_type']=$this->xueli_type;
	$return['from_type']=$this->member_from_type;
	$return['member_from1_type']=$this->member_from1_type;
	$return['member_from2_type']=$this->member_from2_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setstatus4'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['memberid'];
		$member=$post;
		$member['id']=$id;
		$update=array();
		if($post['setstatus']==1 && !$post['worker6id']){
			exi('请先选择销售员！','error');
		}
		if(!$post['setstatus']){
			$update['status']=4;
			$post['type']=3;
			$post['status']=2;
		}elseif($post['setstatus']==2){
			$update['status']=$post['from_type']==1?1:0;
			$post['type']=3;
			$post['status']=2;
		}else{
			$post['type']=4;
			$post['status']=1;
			$update['status']=32;
			$update['worker6id']=$post['worker6id'];
			$update['oldworker6id']=$post['oldworker6id'];
			$can32=true;
		}
		if($post['setstatus']!=2){
			$update['zhun_time']=time();
		}
		$post['remark']='客户已意向';
		$this->model('uplog')->addgenjin_log($post);
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		if($post['setstatus']!=2){
			$this->model('uplog')->upworkerlog($member,4);
		}
		if($can32){
			$member['worker6id']=$post['worker6id'];
			$this->model('uplog')->upworkerlog($member,32);
		}
		exi('操作成功！','','//list/status=all');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['setstatus']=1;
		$item['memberid']=$item['id'];
		unset($item['id']);
	}else{
		exi('请先选择客户','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'处理意向',
	);
	$htmls['edit']=array(
		array('type'=>'radio','title'=>'是否分配销售员?','name'=>'setstatus','options'=>'status'),
		array('type'=>'select','title'=>'分配销售员','name'=>'worker6id','options'=>'workers','show'=>array('setstatus'=>1)),
	);
	$return['workers']=$this->model('worker')->getworkers(6,$_SESSION['storeid']);
	$return['status']=array('暂时不分配','立即分配销售员','未意向退回');	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='worker6id'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		if(!$post['worker6id']){
			exi('请选择销售员！','error');
		}
		$update=array('worker6id'=>intval($post['worker6id']),'up_time'=>time());
		$update['oldworker6id']=$post['oldworker6id'];
		if($post['worker6id']){
			$update['status']=32;
		}
		if(!$post['worker6id']){
			$update['status']=$post['from_type']==1?4:0;
		}
		$ids=$post['ids'];
		foreach($ids as $id){
			$id=intval($id);
			if($id){
				$uplog=array(
					'memberid'=>$id,
					'remark'=>'分配销售员',
					'status'=>1,
					'type'=>2,
				);
				$this->model('uplog')->addgenjin_log($uplog);
				pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
				if($post['worker6id']){
					$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
					$this->model('uplog')->upworkerlog($item,32);
				}
			}
		}
		exi('操作成功！','','//list/status=all');
	}
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}
	if(count($ids)==0){
		exi('请先选择客户','error');
	}
	$item['ids']=$ids;
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'分配销售员',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'分配销售员','name'=>'worker6id','options'=>'workers'),
	);
	$return['workers']=$this->model('worker')->getworkers(6,$_SESSION['storeid']);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export' || $_RQ['op']=='export2'){
	//客户列表
	$return['params']=array();
	$params=array('worker'.$juese_id.'id'=>$workerids,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['jibie_type'] && $_RQ['jibie_type']!='all'){
		$params['jibie_type']=intval($_RQ['jibie_type']);
	}
	$return['params']['jibie_type']=$_RQ['jibie_type']?:'all';
	if($_RQ['status']!='all' && $_RQ['status']!='isall'){
		if($_RQ['status']=='shangmen'){
			$params['zhun_time >']=0;
			$return['params']['status']='shangmen';
		}else{
			$params['status']=intval($_RQ['status']);
			$return['params']['status']=intval($_RQ['status']);
		}
	}elseif($_RQ['status']=='isall'){
		$params['status >']=0;
		$return['params']['status']='isall';
	}else{
		$return['params']['status']='all';
	}
	if($_RQ['from_type']){
		$params['from_type']=$_SESSION['store_type']==3?2:1;
		$return['params']['from_type']=intval($_RQ['from_type']);
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['op']=='export2'){
		$ids=trim($_RQ['ids'],';');
		if($ids){
			$ids=explode(';',$ids);
		}else{
			$ids=array();
		}
		if($_RQ['id']){
			$ids[]=$_RQ['id'];
		}
		if(count($ids)==0){
			exi('请先选择客户','error');
		}
		$params['id']=$ids;
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		if($_RQ['status']=='shangmen'){
			$params['zhun_time between']=array($time_range['tstart'],$time_range['tend']);
		}else{
			$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		}
		$return['params']['createtime']=$time_range;
	}
	$list=pdo_getall('jy_yxw_member',$params,$fields);
	if($list){
		$nowyear=date('Y');
		foreach($list as &$item){
			$item['day']=date('m月d日',$item['createtime']);
			$item['jibie_type']=$this->member_jibie_type[$item['jibie_type']];
			if($item['from_type']==1){
				$item['from_typetitle']=$this->member_from1_type[$item['from1_type']];	
			}elseif($item['from_type']==2){
				$item['from_typetitle']=$this->member_from2_type[$item['from2_type']];	
			}else{
				$item['from_typetitle']=$this->member_from_type[$item['from_type']];	
			}
			$item['status']=$this->member_status[$item['status']];
			$item['xueli']=$this->xueli_type[$item['xueli']];
			$item['sex']=$this->sex[$item['sex']];
			$item['storetitle']=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'title');
			$item['worker5']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['worker5id']),'title');
			$item['worker6']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['worker6id']),'title');	
			$birthtime=strtotime($item['birth'].'-01-01');
			$byear=date('Y',$birthtime);
			if($item['birth']){
				$item['age']=$nowyear-$byear;
			}else{
				$item['age']='无';
			}
		}
	}
	$header=array(
		'day'=>'日期',
		'storetitle'=>'所属门店',
		'worker5'=>'电客人员',
		'worker6'=>'销售员',
		'title'=>'姓名',
		'sex'=>'性别',
		'age'=>'年龄',
		'zhuanye'=>'专业',
		'workdesc'=>'工作经验',
		'mobile'=>'电话',
		'from_typetitle'=>'渠道来源',
		'xueli'=>'学历',
		'gangwei'=>'应聘岗位',
	);
	model('core/tools')->export2excel($header,$list,'客户数据');
}

if($_RQ['op']=='worker5id'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		if(!$post['worker5id']){
			exi('请选择电客员！','error');
		}
		$update=array('worker5id'=>intval($post['worker5id']),'oldworker5id'=>intval($post['oldworker5id']),'up_time'=>time());
		if($post['worker5id'] && ($post['status']==0 || $post['status']==100 || $post['status']==110)){
			$update['status']=1;
			$update['to5time']=time();
		}
		if(!$post['worker5id'] && $post['status']==1){
			$update['status']=0;
		}
		$ids=$post['ids'];
		foreach($ids as $id){
			$id=intval($id);
			if($id){
				$uplog=array(
					'memberid'=>$id,
					'remark'=>'分配电客员',
					'status'=>1,
					'type'=>2,
				);
				$this->model('uplog')->addgenjin_log($uplog);
				pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
				if($post['worker5id']){
					$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
					$this->model('uplog')->upworkerlog($item,1);
				}
			}
		}
		exi('操作成功！','','//list/status=all');
	}
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}
	if(count($ids)==0){
		exi('请先选择客户','error');
	}
	$item['ids']=$ids;
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'分配电客员',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'分配电客员','name'=>'worker5id','options'=>'workers'),
	);
	$return['workers']=$this->model('worker')->getworkers(5,$_SESSION['storeid']);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$this->model('member')->check_del($id,'counts');
			pdo_delete('jy_yxw_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}